package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.dictionary.mapper.AirPortMapper;
import zhongbo.java.air.dictionary.model.AirPort;
import zhongbo.java.air.dictionary.model.AlertZone;
import zhongbo.java.air.dictionary.service.IAirPortService;

import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApiPortServiceImpl implements IAirPortService {

    @Autowired
    private AirPortMapper airPortMapper;

    @Override
    public String addAirPort(@RequestBody AirPort airPort) {
        if (airPort == null) {
            throw new RuntimeException("机场为空");
        }
        airPort.setCreateTime(new Date());
        int insert = airPortMapper.insert(airPort);
        if (insert > 0) {
            return airPort.getId(); // 返回新增飞机id
        }
        throw new RuntimeException("新增机场数据失败");
    }

    @Override
    public AirPort updateAirPort(@RequestBody AirPort airPort) {
        if (airPort == null) {
            throw new RuntimeException("机场为空");
        }
        airPort.setUpdateTime(new Date());
        int update = airPortMapper.updateById(airPort);
        if (update > 0) {
            return airPort;
        }
        throw new RuntimeException("修改机场数据失败");
    }

    @Override
    public int deleteAirPort(@RequestParam(defaultValue = "1") String airId) {
        if (airId == null) {
            throw new RuntimeException("机场id为空");
        }
        int delete = airPortMapper.deleteById(airId);
        if (delete > 0) {
            return delete;
        }
        throw new RuntimeException("删除机场数据失败");
    }

    @Override
    public AirPort getAirPortById(@RequestParam(defaultValue = "1") String airId) {
        if (airId == null) {
            throw new RuntimeException("机场id为空");
        }
        return airPortMapper.selectById(airId);
    }

    @Override
    public List<AirPort> getaAirPortAll() {
        return airPortMapper.getAirCraftAll();
    }

    @Override
    public PageVo<AirPort> page(@RequestParam(required = false)String name,
                                @RequestParam(defaultValue = "1") int pageNumber,
                                @RequestParam(defaultValue = "1")int pageSize) {
        QueryWrapper<AirPort> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like("name",name);
        }
        IPage<AirPort> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirPort> airPortIPage = airPortMapper.selectPage(pageParam,queryWrapper);

        return new PageVo<>(airPortIPage);
    }

    public boolean isHasTheAddId(String airId) {
        List<AirPort> allAir = airPortMapper.getAirCraftAll();
        return allAir.stream().noneMatch(air -> air.getId().equals(airId));
    }
}
